<?php

namespace App\Services\Api;

use App\Models\UserSubscribe;

class SubscribeServices
{

    public function create_subscribe($model,$data){

    }

    /**
     * @param $request
     * @param $subscribeModel \App\Models\UserSubscribe
     */
    public function my_subscribe($request,$user_id,$subscribeModel){
        return $subscribeModel->WhereUserID($user_id)->OrderCreatedAt('desc')->get();
    }

    /**
     * @param $request
     * @param $user_id
     * @param $subscribeModel \App\Models\UserSubscribe
     */
    public function change_subscribe($request,$subscribeModel){
        //dd($request->user()->id);
        $res = $subscribeModel->WhereID($request->subscribe_id)->update([
            'subscribe_status'=>$request->subscribe_status,
        ]);
        return $res;
    }

    public function check_change_subscribe($subscribe_id){
        $SubscribeModel = new UserSubscribe();
        $Subscribe = $SubscribeModel->WhereID($subscribe_id)->select([
            'class_base_info_id',
            'class_time',
            ])
            ->with([
                'class_base_info'=>function($q){
                    $q->select([
                        'id',
                        'advance_cancel_time',
                    ]);
                }
            ])
            ->first();
        $class_time = strtotime($Subscribe->class_time);
        $cancel_time = $class_time - $Subscribe->class_base_infoadvance_cancel_time*60;
        if(time() > $cancel_time){
            return false;
        }
        return true;
    }

}